/*
* Copyright (c) 2012 Kyle Hayward
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "Precompiled.h"

#include "GeometryInstance.h"
#include "Context.h"
#include "Variable.h"
#include "Geometry.h"
#include "Material.h"

using namespace System;

namespace OptixDotNet 
{
	GeometryInstance::GeometryInstance( Context^ context ) : OptixNode( context )
	{
		mInstance = 0;

		pin_ptr<RTgeometryinstance> ptr = &mInstance;
		CheckError( rtGeometryInstanceCreate( context->InternalPtr(), ptr ) );
		mCollection = gcnew NodeCollection<Material^>( this );
	}

	GeometryInstance::GeometryInstance( Context^ context, RTgeometryinstance instance ) : OptixNode( context )
	{
		mInstance = instance;
		mCollection = gcnew NodeCollection<Material^>( this );
	}

	Material^ GeometryInstance::ChildNodes::get( unsigned int index )
	{
		return GetMaterial( index );
	}

	void GeometryInstance::ChildNodes::set( unsigned int index, Material^ node )
	{
		SetMaterial( index, node );
	}

	Material^ GeometryInstance::GetMaterial( unsigned int index )
	{
		if( index >= MaterialCount )
		{
			throw gcnew ArgumentOutOfRangeException( "index" );
		}

		RTmaterial mtrl;
		CheckError( rtGeometryInstanceGetMaterial( mInstance, index, &mtrl ) );

		return gcnew Material( mContext, mtrl );
	}

	void GeometryInstance::SetMaterial( unsigned int index, Material^ material )
	{
		CheckError( rtGeometryInstanceSetMaterial( mInstance, index, material->InternalPtr() ) );
	}

	bool GeometryInstance::RemoveMaterial( Material^ material )
	{
		if( MaterialCount == 0 )
			return false;

		RTmaterial mtrl = material->InternalPtr();

		for( unsigned int i = 0; i < MaterialCount; i++ )
		{
			RTmaterial temp;
			CheckError( rtGeometryInstanceGetMaterial( mInstance, i, &temp ) );
			if( mtrl == temp )
			{
				RemoveMaterialAtIndex( i );
				return true;
			}
		}

		return false;
	}

	OptixDotNet::Geometry^ GeometryInstance::Geometry::get()
	{
		RTgeometry geom;
		CheckError( rtGeometryInstanceGetGeometry( mInstance, &geom ) );

		return gcnew OptixDotNet::Geometry( mContext, geom );
	}

	void GeometryInstance::Geometry::set( OptixDotNet::Geometry^ geometry )
	{
		CheckError( rtGeometryInstanceSetGeometry( mInstance, geometry->InternalPtr() ) );
	}

	Variable^ GeometryInstance::default::get( String^ s )
	{
		const char* str = (const char*)StrToAnsii( s );
		
		RTvariable rtVar;
		CheckError( rtGeometryInstanceQueryVariable( mInstance, str, &rtVar ) );

		if( rtVar == 0 ) 
			CheckError( rtGeometryInstanceDeclareVariable( mInstance, str, &rtVar ) );

		return gcnew Variable( mContext, rtVar );
	}

	void GeometryInstance::default::set( String^ s, Variable^ var )
	{
		if( String::IsNullOrEmpty( s ) )
			throw gcnew Exception( "GeometryInstance Error: Variable name is null or empty" );

		const char* str = (const char*)StrToAnsii( s );

		RTvariable rtVar;
		CheckError( rtGeometryInstanceQueryVariable( mInstance, str, &rtVar ) );

		if( rtVar != 0 && var == nullptr )
		{
			CheckError( rtGeometryInstanceRemoveVariable( mInstance, rtVar ) );
		}
		else
		{
			if( String::IsNullOrEmpty( s ) )
				throw gcnew Exception( "GeometryInstance Error: Variable copying not yet implemented" );
		}
	}

	Variable^ GeometryInstance::default::get( int index )
	{
		if( index < 0 || index >= VariableCount )
			throw gcnew ArgumentOutOfRangeException( "index" );
		
		RTvariable rtVar;
		CheckError( rtGeometryInstanceGetVariable( mInstance, index, &rtVar ) );

		return gcnew Variable( mContext, rtVar );
	}

	void GeometryInstance::default::set( int index, Variable^ var )
	{
		if( index < 0 || index >= VariableCount )
			throw gcnew ArgumentOutOfRangeException( "index" );

		RTvariable rtVar;
		CheckError( rtGeometryInstanceGetVariable( mInstance, index, &rtVar ) );

		if( var == nullptr )
		{
			CheckError( rtGeometryInstanceRemoveVariable( mInstance, rtVar ) );
		}
		else
		{
			throw gcnew Exception( "GeometryInstance Error: Variable copying not yet implemented" );
		}
	}
}